package jdbc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.UUID;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import jdbc.dao.ShopDao;
import jdbc.entity.ShopEntity;
import jdbc.service.ShopService;

public class JdbcTempLate {
	ApplicationContext context = new ClassPathXmlApplicationContext("jdbc/applicationContext.xml");
	@Test
	public void test() {
		ShopEntity shopEntity = new ShopEntity();
		// spring 上下文：容器的对象
		ShopDao shopDao = context.getBean(ShopDao.class);
	}

	@Test
	public void testAdd() {
		//1、添加到数据库
		String goodsName = UUID.randomUUID().toString();
		ShopEntity shopEntity = new ShopEntity(goodsName,"商品编码","110","test1");
		ShopService shopService = context.getBean(ShopService.class);
		shopService.addShop(shopEntity);
		//2、根据条件查询这条数据
		ShopEntity newShoEntity = shopService.getShop(goodsName);
		//3、比较这两个对象中的值是否相等
		assertNotNull(newShoEntity);
		assertEquals(shopEntity.getsName(), newShoEntity.getsName());
		assertEquals(shopEntity.getsNumber(), newShoEntity.getsNumber());
		assertEquals(shopEntity.getCreateName(), newShoEntity.getCreateName());
		assertEquals(shopEntity.getsPrice(), newShoEntity.getsPrice());
	}

	@Test
	public void testUpdate() {
		String goodsName = UUID.randomUUID().toString();
		ShopEntity shopEntity = new ShopEntity(goodsName,"商品编码","110","test1");
		ShopService shopService = context.getBean(ShopService.class);
		shopService.addShop(shopEntity);
		//2、根据条件查询这条数据
		shopService.updateShop(shopEntity);
		ShopEntity newShoEntity = shopService.getShop(goodsName);
		//3、比较这两个对象中的值是否相等
		assertEquals("hai",newShoEntity.getsName());
		assertEquals("001",newShoEntity.getsNumber());
		assertEquals("100",newShoEntity.getCreateName());
		assertEquals("king",newShoEntity.getsPrice());
	}

	@Test
	public void testDelete() {
		//1、添加到数据库
		String goodsName = UUID.randomUUID().toString();
		ShopEntity shopEntity = new ShopEntity(goodsName,"商品编码","110","test1");
		ShopService shopService = context.getBean(ShopService.class);
		shopService.addShop(shopEntity);
		//2、删除这条数据
		shopService.deleteShop(goodsName);
		//3、查询数据删除状态是否变为Y
		ShopEntity newShoEntity = shopService.getShop(goodsName);
		assertEquals("Y", newShoEntity.getDeleteLogo());
	}

	@Test
	public void testGet() {
		String goodsName = UUID.randomUUID().toString();
		ShopEntity shopEntity = new ShopEntity(goodsName,"商品编码","110","test1");
		ShopService shopService = context.getBean(ShopService.class);
		shopService.addShop(shopEntity);
		//2、查询这条数据
		shopService.getShop(goodsName);
		//3、查询数据状态是否一样
		ShopEntity newShoEntity = shopService.getShop(goodsName);
		assertEquals(shopEntity.getsName(), newShoEntity.getsName());
		assertEquals(shopEntity.getsNumber(), newShoEntity.getsNumber());
		assertEquals(shopEntity.getCreateName(), newShoEntity.getCreateName());
		assertEquals(shopEntity.getsPrice(), newShoEntity.getsPrice());
	}
}
